package com.hz.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import com.hz.dbconn.DBConnect;
import com.hz.model.ActType;

public class ActTypeControl {
    /*
     * 活动类型
     */
    public static boolean AddActType(ActType at) {
	DBConnect dbc = new DBConnect();
	String sql = "insert into acttype(explanation,typeName) value(?,?)";
	dbc.prepareStatement(sql);
	try {
	    dbc.setString(1, at.getDescribe());
	    dbc.setString(2, at.getTypeName());
	   
	    dbc.executeUpdate();
	    return true;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("插入失败！");
	    return false;
	}finally{
	    dbc.close();
	}
    }
    /*
     * 活动类型名称
     */
    public static String getActTypeName(int tid) {
	DBConnect dbc = new DBConnect();
	String sql = "select typeName from actType where id='"+tid+"'";
	String typeName = null;
	dbc.prepareStatement(sql);
	try {
	    ResultSet rs = dbc.executeQuery();
	    if(rs.next()){
		typeName = rs.getString("typeName");
	    }
	    return typeName;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("获取活动类型名失败！");
	    return null;
	}finally{
	    dbc.close();
	}
    }
    
    /*
     * 活动类型名称
     */
    public static ActType getActType(int tid) {
	DBConnect dbc = new DBConnect();
	String sql = "select * from actType where id='"+tid+"'";
	ActType a = null;
	dbc.prepareStatement(sql);
	try {
	    ResultSet rs = dbc.executeQuery();
	    if(rs.next()){
		a = new ActType();
		a.setTypeName(rs.getString("typeName"));
		a.setId(rs.getInt("id"));
		a.setDescribe(rs.getString("explanation"));
	    }
	    return a;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("获取活动类型名失败！");
	    return null;
	}finally{
	    dbc.close();
	}
    }
    
    //删除
    public static boolean deleteActType(int id){
	DBConnect dbc = new DBConnect();
	String sql = "delete from acttype where id=?";
	dbc.prepareStatement(sql);
	try{
		dbc.setInt(1, id);
		dbc.executeUpdate();
		return true;
	}catch(SQLException e){
		e.printStackTrace();
		return false;
	}finally{
		dbc.close();
	}
    }
    //更新
  //update project
	public static boolean updateActType(String actname,String desc,int id){
		DBConnect dbc = new DBConnect();
		String sql = "update acttype set typeName='"+actname+"', explanation='"+desc+"' where id='"+id+"'";
		dbc.prepareStatement(sql);
		try{
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
    /*
     * 遍历所有活动类型（供查询时选择）
     */
    public static Collection<ActType> getAllActTypes() {
	DBConnect dbc = new DBConnect();
	Collection<ActType> atlist = new ArrayList<ActType>();
	ActType at = null;
	String sql = "select * from actType"; 
	dbc.prepareStatement(sql);
	ResultSet rs = null;
	try {
	    rs = dbc.executeQuery();
	    while(rs.next()){
		at = new ActType();
		at.setId(rs.getInt("id"));
		at.setDescribe(rs.getString("explanation"));
		at.setTypeName(rs.getString("typeName"));
		atlist.add(at);
	    }
	    return atlist;
	} catch (Exception e) {
	    // TODO: handle exception
	    e.printStackTrace();
	    System.out.println("遍历活动类型失败！");
	    return null;
	}finally{
	    dbc.close();
	}
    }
    

}
